package com.lngsyz.ywtb.biz.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lngsyz.ywtb.entity.biz.CompanyInfo;
import com.lngsyz.ywtb.entity.source1.SystemCompany;
import com.lngsyz.ywtb.mapper.source1.SystemCompanyMapper;

import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;

@Service
public class SystemCompanyService {
    
	@Autowired
	SystemCompanyMapper systemCompanyMapper;
	
    public List<SystemCompany> findByCondition(String districtCity, String district, String name, int page, int size){
    	Map<String, Object> param = new HashMap<String, Object>();
    	param.put("districtCity", districtCity);
    	if(StringUtils.isNotBlank(district)) {
    		param.put("district", district);
    	}
    	if(StringUtils.isNotBlank(name)) {
    		param.put("name", name);
    	}
    	param.put("minSize", page <= 0 ? 0 : (page-1) * size);
		param.put("maxSize", page <= 0 ? size : page * size);
    	List<SystemCompany> lists = systemCompanyMapper.findByCondition(param);
    	return lists;
    }
    
    public int findTotal(String districtCity, String district, String name){
    	Map<String, Object> param = new HashMap<String, Object>();
    	param.put("districtCity", districtCity);
    	if(StringUtils.isNotBlank(district)) {
    		param.put("district", district);
    	}
    	if(StringUtils.isNotBlank(name)) {
    		param.put("name", name);
    	}
    	int total = systemCompanyMapper.findTotal(param);
    	return total;
    }
    
    public SystemCompany findById(String id) {
    	if(StringUtils.isNotBlank(id)) {
    		return systemCompanyMapper.findById(id);
    	} else {
    		return null;
    	}
    }
    
    public String findNameById(String id) {
    	String rtn = "";
    	SystemCompany sc = systemCompanyMapper.findById(id);
    	if(sc != null) {
    		rtn = sc.getName();
    	}
    	return rtn;
    }
    
    public CompanyInfo findInfo(String id) {
    	CompanyInfo rtn = null;
    	if(StringUtils.isNotBlank(id)) {
    		SystemCompany sc = systemCompanyMapper.findById(id);
    		if(sc != null) {
    			rtn = new CompanyInfo();
    			rtn.setName(sc.getName());
    			rtn.setAddress(sc.getAddress());
    			rtn.setContactMan(sc.getContactman());
    			rtn.setContactPhone(sc.getContactphone());
    			rtn.setCoordinateX(sc.getCoordinateX());
    			rtn.setCoordinateY(sc.getCoordinateY());
    		}
    	}
    	return rtn;
    }
    
    public SystemCompany findByInstCode(String instCode) {
    	Map<String, Object> param = new HashMap<String, Object>();
		param.put("businesslicense", StringUtils.upperCase(instCode));
    	param.put("minSize", 0);
		param.put("maxSize", 1);
    	List<SystemCompany> lists = systemCompanyMapper.findByCondition(param);
    	if(lists != null && lists.size() > 0) {
    		return lists.get(0);
    	} else {
    		return null;
    	}
    }
}
